package controller.WorkManagement;

import entity.Employment;
import entity.JobTendency;
import entity.User;
import entity.WorkExp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.EnterpriseManagement.EnterpriseService;
import service.SchoolRecruitManagement.SchoolRecruitService;
import service.WorkManagement.WorkService;
import util.ResponseMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/Work")
public class WorkController {

    @Autowired
    private WorkService workService;

    @Autowired
    private SchoolRecruitService schoolRecruitService;

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/upload_workExp")
    public ResponseMessage upload_workExp(String time, String position, String company, String department, String other, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        WorkExp workExp = new WorkExp();
        workExp.setUserId(Current_logged_in_user.getUserId());
//        workExp.setUserId("13163357255");
        workExp.setTime(time);
        workExp.setPosition(position);
        workExp.setCompany(company);
        workExp.setDepartment(department);
        workExp.setOther(other);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String insertTime = df.format(new Date());
        workExp.setInsertTime(insertTime);//获取当前时间
        boolean flag = workService.addWorkExp(workExp);
        if (flag) {
            int expId = workService.findExpIdByTimeAndUserId(insertTime, Current_logged_in_user.getUserId());
//            int expId = workService.findExpIdByTimeAndUserId(insertTime, "13163357255");
//            String url = "http://47.96.104.134:8080/oneschool/Work/show_justWorkExp?expId="+expId;
            return new ResponseMessage(ResponseMessage.SUCCESS, "经验上传成功", expId);
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/show_justWorkExp")
    public ResponseMessage show_justWorkExp(String expId, HttpServletRequest request) {
        if (expId.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "expId不能为空");
        }
        LinkedHashMap<String, Object> data = workService.findOneWorkExpByExpId(expId);
        try {
            data.put("time", String.valueOf(data.get("time")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", data);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/upJobTendency")
    public ResponseMessage upJobTendency(String paycheckMin, String paycheckMax, String position, String address, String selfAssessment, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        JobTendency jobTendency = new JobTendency();
        jobTendency.setUserId(Current_logged_in_user.getUserId());
        jobTendency.setPaycheckMin(paycheckMin);
        jobTendency.setPaycheckMax(paycheckMax);
        jobTendency.setPosition(position);
        jobTendency.setAddress(address);
        jobTendency.setSelfAssessment(selfAssessment);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String insertTime = df.format(new Date());
        jobTendency.setInsertTime(insertTime);
        boolean flag = workService.addJobTendency(jobTendency);
        if (flag) {
            int TendencyId = workService.findTendencyIdByTimeAndUserId(insertTime, Current_logged_in_user.getUserId());
//            String url = "http://47.96.104.134:8080/oneschool/Work/show_justJobTendency?TendencyId="+TendencyId;
            return new ResponseMessage(ResponseMessage.SUCCESS, "上传成功", TendencyId);
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/show_justJobTendency")
    public ResponseMessage show_justJobTendency(String TendencyId, HttpServletRequest request) {
        if (TendencyId.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "expId不能为空");
        }
        LinkedHashMap<String, Object> data = workService.findOneJobTendencyByExpId(TendencyId);
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", data);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/showWorkExp")
    public ResponseMessage showWorkExp(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<WorkExp> list = workService.showWorkExp(Current_logged_in_user.getUserId());
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setTime(list.get(i).getTime());
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", list);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/showJobTendency")
    public ResponseMessage showJobTendency(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<JobTendency> list = workService.showJobTendency(Current_logged_in_user.getUserId());
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", list);
    }

    /**
     * @param name（职位名）
     * @param type（职位类型）
     * @param expRequest（经验要求）
     * @param degreeRequest（学历要求）
     * @param address（工作地点）
     * @param describe（职位简述）
     * @param key（职位简要描述）
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/uploadRecruit")
    public ResponseMessage uploadRecruit(String name, String type, String expRequest, String degreeRequest, String address, String describe, String key, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        if (Current_logged_in_user.getType() != 3) {
            return new ResponseMessage(ResponseMessage.ERROR, "当前用户非企业人员，不能发布招聘活动");
        }
        Employment employment = new Employment();
        //根据用户手机号查询enterpriseId
        String enterpriseId = schoolRecruitService.findEnterpriseId(Current_logged_in_user.getUserId());
        if (enterpriseId.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "当前用户未完善信息，不能发布招聘活动");
        }
        employment.setEnterpriseId(enterpriseId);
        employment.setName(name);
        employment.setType(type);
        employment.setExpRequest(expRequest);
        employment.setDegreeRequest(degreeRequest);
        employment.setAddress(address);
        employment.setDescribe(describe);
        employment.setKey(key);
        // String insertTime,获取系统当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String insertTime = df.format(new Date());
        employment.setInsertTime(insertTime);
        boolean flag = workService.addRecruit(employment);
        if (flag) {
            String EmploymentId = workService.findEmploymentId(enterpriseId, insertTime);
            return new ResponseMessage(ResponseMessage.SUCCESS, "发布成功", EmploymentId);
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/showJustRecruit")
    public ResponseMessage showJustRecruit(String recruitId,HttpServletRequest request) {
        if (recruitId.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "recruitId不能为空");
        }
        LinkedHashMap<String, Object> data = workService.findOneRecruitById(recruitId);
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功",data);
    }

}//end class
